//
//  keyboardViewController.m
//  keyboard
//
//  Created by Sergio on 19/01/11.
//  Copyright HappyBee 2011. All rights reserved.
//

#import "keyboardViewController.h"

@implementation keyboardViewController
@synthesize textFields;
@synthesize auxTextField;

- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 20)];
	textfield.backgroundColor = [UIColor whiteColor];
	textfield.delegate = self;
	[self.view addSubview:textfield];
	[textfield release];
	
	UITextField *textfield2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 20)];
	textfield2.backgroundColor = [UIColor whiteColor];
	textfield2.delegate = self;
	[self.view addSubview:textfield2];
	[textfield2 release];
	
	UITextField *textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 20)];
	textfield3.backgroundColor = [UIColor whiteColor];
	textfield3.delegate = self;
	[self.view addSubview:textfield3];
	[textfield3 release];
	
	UITextField *textfield4 = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 100, 20)];
	textfield4.backgroundColor = [UIColor whiteColor];
	textfield4.delegate = self;
	[self.view addSubview:textfield4];
	[textfield4 release];
	
	UITextField *textfield5 = [[UITextField alloc] initWithFrame:CGRectMake(50, 250, 100, 20)];
	textfield5.backgroundColor = [UIColor whiteColor];
	textfield5.delegate = self;
	[self.view addSubview:textfield5];
	[textfield5 release];
	
	UITextField *textfield6 = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 100, 20)];
	textfield6.backgroundColor = [UIColor whiteColor];
	textfield6.delegate = self;
	[self.view addSubview:textfield6];
	[textfield6 release];
	
	[self initializeForCustomInputView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc 
{
    [super dealloc];
}

#pragma mark -
#pragma mark InputAccesoryView Custom

#define kMinOffset 60

- (void)viewWillAppear:(BOOL)animated
{
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
												 name:UIKeyboardWillShowNotification
											   object:self.view.window]; 
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
												 name:UIKeyboardWillHideNotification
											   object:self.view.window]; 
}

- (void)viewWillDisappear:(BOOL)animated
{
	[[NSNotificationCenter defaultCenter] removeObserver:self 
													name:UIKeyboardWillShowNotification
												  object:nil]; 
	
	[[NSNotificationCenter defaultCenter] removeObserver:self
													name:UIKeyboardWillHideNotification 
												  object:nil];
}

- (void)initializeForCustomInputView
{
	textFields = [NSMutableArray new];
	
	for (UIView *v in self.view.subviews)
	{
		if ([v isKindOfClass:[UITextField class]])
		{
			UITextField *field = (UITextField *)v;
			[textFields addObject:field];
		}
	}
	
	if (auxTextField == nil)
	{
		auxTextField = [textFields objectAtIndex:0];
	}
}

- (UIView *)inputAccessoryView
{
	[super inputAccessoryView];
	
	UIView *dismiss = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
	UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
	toolbar.barStyle = UIBarStyleBlack;
	
	UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous"
																 style:UIBarButtonItemStyleBordered
																target:self 
																action:@selector(previousAction:)];
	
	UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"   Next   "
															 style:UIBarButtonItemStyleBordered
															target:self 
															action:@selector(nextAction:)];
	
	UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
																			   target:self
																			   action:nil];
	
	UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"   Done   "
															 style:UIBarButtonItemStyleBordered
															target:self 
															action:@selector(doneAction:)];
	
	NSArray *items = [[NSArray alloc] initWithObjects:previous, next, flexSpace, done, nil];
	[toolbar setItems:items];
	[items release];
	[previous release];
	[next release];
	[flexSpace release];
	[done release];
	[dismiss addSubview:toolbar];
	[toolbar release];
	
	return dismiss;
}

- (void)previousAction:(id)sender
{
	int index = [self indexOfTextField:auxTextField];
	
	UITextField *previousField = nil;
	
	if (index == 0)
	{
		previousField = [textFields objectAtIndex:[textFields count] - 1];
	}
	else 
	{
		previousField = [textFields objectAtIndex:index - 1];
	}
	
	auxTextField = previousField;
	[self setViewMovedUp:YES];
	[previousField becomeFirstResponder];
}

- (void)nextAction:(id)sender
{
	int index = [self indexOfTextField:auxTextField];
	
	UITextField *next = nil;
	
	if (index >= [textFields count] - 1)
	{
		next = [textFields objectAtIndex:0];
	}
	else 
	{
		next = [textFields objectAtIndex:index + 1];
	}

	auxTextField = next;
	[self setViewMovedUp:NO];
	[next becomeFirstResponder];
}

- (void)doneAction:(id)sender
{
	[auxTextField resignFirstResponder];
}

- (int)indexOfTextField:(UITextField *)field
{
	int i = 0;
	
	for (UITextField *f in textFields)
	{
		if ([f isEqual:field])
		{
			break;
		}
		
		i++;
	}
	
	return i;
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
{
	[auxTextField resignFirstResponder];
	auxTextField = theTextField;
	
	return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
	auxTextField = textField;

	if (self.view.frame.origin.y + auxTextField.frame.origin.y >= kMinOffset)
        [self setViewMovedUp:YES];
}

- (void)setViewMovedUp:(BOOL)movedUp
{
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3];
	
	CGRect rect = self.view.frame;
	
	if (movedUp)
	{
		rect.origin.y = kMinOffset - auxTextField.frame.origin.y;
	}
	else
	{
		rect.origin.y = 0;
	}
	
	self.view.frame = rect;

	[UIView commitAnimations];
}

- (void)keyboardWillShow:(NSNotification *)notif
{
	if ([auxTextField isFirstResponder] && auxTextField.frame.origin.y + self.view.frame.origin.y >= kMinOffset)
	{
		[self setViewMovedUp:YES];
	}
	else if (![auxTextField isFirstResponder] && auxTextField.frame.origin.y  + self.view.frame.origin.y < kMinOffset)
	{
		[self setViewMovedUp:NO];
	}
}

- (void)keyboardWillHide:(NSNotification *)notif
{
	if (self.view.frame.origin.y < 0)
	{
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.3];
		self.view.frame = CGRectMake(0, 0, 320, 480);
		[UIView commitAnimations];
	}
}

@end